// pages/mch/contract/index.js
const App = getApp()
import Kits from '../../../utils/kits'

Page({
  data: {
    init: false,
    loading: false,
    triggered: true,
    CustomBar: App.Global.CustomBar,

    page: 1,
    size: 8,
    time: new Date().getTime(),
    pager: {
      now: 1,
      row: 0,
      all: 0,
      fms: 0,
    },
    items: []
  },
  onLoad() {
    this.execLoad(false)
  },
  doPulldown() {
    this.data.page = 1
    this.execLoad(true)
  },
  doReachBottom() {
    if (this.data.page >= this.data.pager.all) {
      return
    }
    this.data.page = this.data.page + 1
    this.execLoad(true)
  },

  execLoad(pulldown) {
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/merchant/contract/list',
      body: {
        pager: {
          page: this.data.page,
          size: this.data.size,
          time: this.data.time
        },
        query: {
          state: 0
        }
      },
      fnSucc: resp => {
        if (resp.data.pager.now == 1) {
          this.data.time = resp.data.pager.fms
        }
        resp.data.items.forEach(item => {
          item.order.amountStr = Kits.fmtMoneyString(item.order.amount)
        })

        this.setData({
          init: true,
          loading: false,
          triggered: false,
          pager: resp.data.pager,
          items: resp.data.pager.now == 1 ? resp.data.items : this.data.items.concat(resp.data.items)
        })
      },
      fnComp: () => {
        if (pulldown) {
          this.triggerEvent('stopPullDown', {})
        }
      }
    })
  },
  toPage(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.uri
    })
  },
  toViewCon(e) {
    let con = e.currentTarget.dataset.con
    wx.navigateTo({
      url: `/pages/funcs/fdd?url=${encodeURIComponent(con.view)}`,
    })
  }

})